﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LabLibary.ExtractData
{
    public static class CobasC311
    {
        public static List<ObjResults> ExtactData(string fileName)
        {
            string _SID = string.Empty;
            DateTime _Day = DateTime.Now;

            List<ObjResults> _lstResults = new List<ObjResults>();
            ObjResults _objResults = new ObjResults();


            string result = string.Empty;
            try
            {
                using (StreamReader sr = new StreamReader(fileName))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        try
                        {
                            if (line[0] == 'O')
                            {
                                string[] _tmpSplit = line.Split('|');
                                result += "ID: " + _tmpSplit[2].Trim() + "\n";

                                _SID = _tmpSplit[2].Trim();

                                string _date =_tmpSplit[23].Trim();
                                string _nam = _date.Remove(4, 10);
                                string _thang = _date.Replace(_nam, string.Empty).Remove(2, 8);
                                string _ngay = _date.Replace(_nam, string.Empty).Replace(_thang, string.Empty).Remove(2,6);                                

                                _Day = new DateTime(int.Parse(_nam), int.Parse(_thang), int.Parse(_ngay), 0, 0, 0);

                            }

                            if (line[0] == 'R')
                            {
                                string[] _tmpSplit = line.Split('|');
                                result += "LabID: " + _tmpSplit[2].Trim().Replace("^", string.Empty).Replace("/", string.Empty);
                                result += " - Value: " + _tmpSplit[3].Trim() + "\n";

                                string _May = "15";
                                string _MaMay = _tmpSplit[2].Trim().Replace("^", string.Empty).Replace("/", string.Empty);
                                string _GiaTri = _tmpSplit[3].Trim();
                                DateTime _TGTra = DateTime.Now;


                                //_lstResults.Add(new ObjResults(_Ngay, _SID, _tmpSplit[2].Trim().Replace("^", string.Empty).Replace("/", string.Empty), _tmpSplit[3].Trim()), DateTime.Now);
                                _lstResults.Add(new ObjResults(_Day, _SID, _May, _MaMay, _GiaTri, _TGTra));
                            }
                        }
                        catch { }
                    }

                    try
                    {
                        sr.Close();
                        File.Delete(fileName);
                    }
                    catch { }
                }
            }
            catch
            {
                result = null;
            }
            return _lstResults;
        }
    }
}
